package com.zrc.module_home.fragment

import android.Manifest
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.zrc.superframe_aop.annotation.Permission
import com.zrc.superframe_base.ARouterHub
import com.zrc.superframe_base.mvx.mvvm.BaseMvvmFragment
import com.zrc.module_home.BR
import com.zrc.module_home.R
import com.zrc.module_home.databinding.FragmentHomeBinding
import com.zrc.module_home.viewmodel.HomeVM
import com.zrc.superframe_ui.stateview.state.EmptyState
import com.zrc.superframe_ui.stateview.state.ErrorState
import com.zrc.superframe_ui.stateview.state.LoadingState
import com.zrc.superframe_ui.stateview.state.SuccessState
import kotlinx.android.synthetic.main.fragment_home.*

/**
 * @Author: superman
 * @CreateTime: 6/27/21
 * @Describe:
 */
@Route(path = ARouterHub.FRAGMENT_MAIN, name = "首页")
class HomeFragment : BaseMvvmFragment<FragmentHomeBinding, HomeVM>() {
    private lateinit var adapter:DataBindingAdapter
    private var page:Int=1
    private var isRefresh=true

    override fun getVariableId() = BR.viewModel

    override fun getLayoutId() = R.layout.fragment_home

    override fun getViewModel() = HomeVM(this)

    override fun initObserver() {
        super.initObserver()
        changeSmartRefreshLayout()
        mBinding.smartRefreshLayout.setOnRefreshListener {
            page=1
            isRefresh=true
            mViewModel.getData(page)
        }

        mBinding.smartRefreshLayout.setOnLoadMoreListener {
            isRefresh=false
            mViewModel.getData(page)
        }

        mBinding.recyclerView.layoutManager = LinearLayoutManager(context)
        adapter = DataBindingAdapter()
        //adapter.setEmptyView(loadingView)
        mBinding.recyclerView.adapter = adapter
        mViewModel.getData(page)

        mBinding.container.show<LoadingState> {
            it.setLoadingMsg("加载中...")
        }
        mViewModel.demoLiveData.observe(this, {
            if (!it.isNullOrEmpty()){
                if (isRefresh){
                    adapter.setList(it)
                }else{
                    adapter.addData(it)
                }
                mBinding.container.show<SuccessState> {  }
                page++
            }else{
                if (it==null){
                    mBinding.container.show<ErrorState> {
                        it.setErrorIcon(R.drawable.vector_error)
                        it.setErrorMsg("出错了")
                        it.retry {
                            mBinding.smartRefreshLayout.autoRefresh()
                        }
                    }
                }else{
                    mBinding.container.show<EmptyState> {
                        it.setEmptyIcon(R.drawable.vector_error)
                        it.setEmptyMsg("暂无数据")
                    }
                }
            }
            changeSmartRefreshLayout()
        })
    }

    private fun changeSmartRefreshLayout() {
        try {
            when (smartRefreshLayout.state) {
                RefreshState.Loading -> {
                    smartRefreshLayout.finishLoadMore()
                }
                RefreshState.Refreshing -> {
                    smartRefreshLayout.finishRefresh()
                }
            }
            //smartRefreshLayout.setEnableLoadMore(canLoadMore)
        }catch (e:Exception){

        }
    }

    override fun onClick(view: View) {
        handleRequestPermission()
    }

    @Permission(
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.CALL_PHONE
    )
    private fun handleRequestPermission() {

    }
}